package com.hornsun.role.api;


import com.hornsun.data.constant.error.AccountError;
import com.hornsun.data.constant.error.CommonError;
import com.hornsun.data.constant.types.ReturnType;
import com.hornsun.data.dbo.Role;
import com.hornsun.data.repository.ClientRepository;
import com.hornsun.data.repository.PermissionRepository;
import com.hornsun.data.repository.RoleRepository;
import com.hornsun.role.dto.RoleDTO;
import com.hornsun.role.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by 编译中... on 2018/7/11.
 */
@RestController
@RequestMapping("/api/role")
public class RoleAPI {
    @Autowired
    private RoleRepository roleRepository;

    @Autowired
    private PermissionRepository permissionRepository;

    @Autowired
    private RoleService roleService;

    @Autowired
    private ClientRepository clientRepository;

    @GetMapping("/getInfo")
    public ResponseEntity<?> getInfo(){

        List<RoleDTO> roleDTOList = roleService.getAllRoleInfo();
        return new ResponseEntity<Object>(roleDTOList, HttpStatus.OK);
    }


    @GetMapping("/getByPermission")
   public ResponseEntity<ReturnType> getByPermission(@RequestParam("permission")Integer permission){

        List<Role> roleList = roleRepository.getTotalRole();
        List<Role> list = new ArrayList<>();
        for (Role role : roleList){
            if ((role.getPermission()&permission)!=0){
                    list.add(role);
            }
        }
        return new ResponseEntity<ReturnType>(new ReturnType(list),HttpStatus.OK);
    }


    @GetMapping("/getTotalPermission")
    public ResponseEntity<?> getTotalPermission(){
        return new ResponseEntity<Object>(permissionRepository.getTotal(),HttpStatus.OK);
    }

    @PostMapping("/permission/add")
    public ResponseEntity<?> addRole(@RequestParam("name")String name ,@RequestParam("permission")int permission){
            if (name.isEmpty()){
                return new ResponseEntity<Object>(CommonError.NOT_EXIST.getCode(),HttpStatus.OK);
            }
            Role role = new Role();
            role.setRoleGuid(UUID.randomUUID().toString());
            role.setRoleName(name);
            role.setPermission(permission);
            roleRepository.add(role);
            List<RoleDTO> roleDTOList = roleService.getAllRoleInfo();
            return new ResponseEntity<Object>(roleDTOList,HttpStatus.OK);
    }

    @PostMapping("/permission/delete")
    public ResponseEntity<?> deleteRole(String[] guids){
        if (guids==null){
            return new ResponseEntity<Object>(HttpStatus.OK);
        }
        List<Role> roleList = new ArrayList<>();
        for (int i=0;i<guids.length;i++){
            if(hasClient(guids[i])||roleService.whetherSuper(guids[i])){
                roleList.add(roleRepository.getByGuid(guids[i]));
            }else {
                roleRepository.delete(guids[i]);
            }
        }
        return new ResponseEntity<Object>(roleList,HttpStatus.OK);
    }

    @PostMapping("/permission/update")
    public ResponseEntity<?> update(@RequestParam("guid")String guid,@RequestParam("name")String name,@RequestParam("permission")int permission){
        if (roleService.whetherSuper(guid)){
            return new ResponseEntity<Object>(AccountError.IS_SUPER.getCode(),HttpStatus.OK);
        }
        Role role = new Role();
        role.setRoleGuid(guid);
        role.setRoleName(name);
        role.setPermission(permission);
        roleRepository.update(role);
        List<RoleDTO> roleDTOList = roleService.getAllRoleInfo();
        return new ResponseEntity<Object>(roleDTOList,HttpStatus.OK);
    }


    private boolean hasClient(String guid){
        return clientRepository.countByRole(guid)>0;
    }


}
